package com.emenu.common;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;


public class GetWebServiceData {
	private static final String NameSpace = "http://tempuri.org/";
	private static final String MethodQueryJson = "QueryJson";
	private static final String MethodIsUser = "SysUser_IsUser";
	private static final String MethodImage = "GetImageLarger";
	private static final String MethodExcute = "Excute";
	private static final String MethodPrintBusRetailDetail = "RetailDetail_GetRecordStatus";

	// 代码中使用localhost或者127.0.0.1来访问，都是访问模拟器自己
	// 在模拟器上用10.0.2.2访问你的电脑本机
	// private static String url;// = "http://10.0.2.2/Ws_jpdzcp/WS_DZCP.asmx";

	public GetWebServiceData() {

	}

	public static String WSJson(String url, String sql) {

		// Configuration cf = new Configuration();
		// url = cf.getValue("url");
		String soapAction = NameSpace + MethodQueryJson;
		String result = "";

		try {

			// 指定WebService的命名空间和调用的方法名

			SoapObject request = new SoapObject(NameSpace, MethodQueryJson);
			// step2设置调用方法的参数值

			request.addProperty("strSQL", sql);

			// 生成调用WebService方法的SOAP请求信息。
			// 该信息由SoapSerializationEnvelope对象描述

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

			SoapEnvelope.VER11);
			// 第1步创建的SoapObject对象。
			envelope.bodyOut = request;

			// 设置webservice是.Net
			envelope.dotNet = true;

			// 等于envelope.bodyOut = request;

			// envelope.setOutputSoapObject(request);

			// step4 创建HttpTransportSE对象,指定url

			HttpTransportSE ht = new HttpTransportSE(url);

			// call方法调用WebService方法

			// ht.call(soapAction, envelope);

			// //防止连接时间过期////////////////////////////

			try {
				Thread.sleep(300);
				ht.call(soapAction, envelope);

			} catch (SocketTimeoutException e1) {
				// TODO Auto-generated catch block
				try {
					ht.call(soapAction, envelope);
				} catch (SocketTimeoutException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			// //////////////////////////////

			// 若返回值有多个，则用SoapObject取，用getProperty来获得每个返回值
			// SoapObject soapObject = (SoapObject) envelope.getResponse();
			// SoapPrimitive value = (SoapPrimitive)soapObject.getProperty(0);

			// 若返回值是单个值，则只能用SoapPrimitive取，此时若用SoapObject强制转换会出异常
			if (envelope.getResponse() != null) {

				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

				result = response.toString();
			}

		} catch (Exception e) {

			result = e.getMessage();

		}

		return result;

	}

	// 返回PrintBusRetailDetail
	public static String WSPrintBusRetailDetailJson(String url,
			String table_code, String time_code) {

		// Configuration cf = new Configuration();
		// url = cf.getValue("url");
		String soapAction = NameSpace + MethodPrintBusRetailDetail;
		String result = "";

		try {

			// 指定WebService的命名空间和调用的方法名

			SoapObject request = new SoapObject(NameSpace,
					MethodPrintBusRetailDetail);
			// step2设置调用方法的参数值

			request.addProperty("table_code", table_code);
			request.addProperty("time_code", time_code);

			// 生成调用WebService方法的SOAP请求信息。
			// 该信息由SoapSerializationEnvelope对象描述

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

			SoapEnvelope.VER11);
			// 第1步创建的SoapObject对象。
			envelope.bodyOut = request;

			// 设置webservice是.Net
			envelope.dotNet = true;

			// 等于envelope.bodyOut = request;

			// envelope.setOutputSoapObject(request);

			// step4 创建HttpTransportSE对象,指定url

			HttpTransportSE ht = new HttpTransportSE(url);

			// call方法调用WebService方法

			ht.call(soapAction, envelope);

			// 若返回值有多个，则用SoapObject取，用getProperty来获得每个返回值
			// SoapObject soapObject = (SoapObject) envelope.getResponse();
			// SoapPrimitive value = (SoapPrimitive)soapObject.getProperty(0);

			// 若返回值是单个值，则只能用SoapPrimitive取，此时若用SoapObject强制转换会出异常
			if (envelope.getResponse() != null) {

				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

				result = response.toString();
			}

		} catch (Exception e) {

			result = e.getMessage();

		}

		return result;

	}

	// 检查登录用户
	public static boolean WSIsUser(String url, String usercode, String password) {
		// Configuration cf = new Configuration();
		// url = cf.getValue("url");

		String soapAction = NameSpace + MethodIsUser;
		boolean result = false;

		try {

			// 指定WebService的命名空间和调用的方法名

			SoapObject request = new SoapObject(NameSpace, MethodIsUser);
			// step2设置调用方法的参数值

			request.addProperty("usercode", usercode);
			request.addProperty("password", password);

			// 生成调用WebService方法的SOAP请求信息。
			// 该信息由SoapSerializationEnvelope对象描述

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

			SoapEnvelope.VER11);
			// 第1步创建的SoapObject对象。
			envelope.bodyOut = request;

			// 设置webservice是.Net
			envelope.dotNet = true;

			// 等价于envelope.bodyOut = request;
			// envelope.setOutputSoapObject(request);

			// step4 创建HttpTransportSE对象,指定url

			HttpTransportSE ht = new HttpTransportSE(url);

			// call方法调用WebService方法

			ht.call(soapAction, envelope);

			// 若返回值有多个，则用SoapObject取，用getProperty来获得每个返回值
			// SoapObject soapObject = (SoapObject) envelope.getResponse();
			// SoapPrimitive value = (SoapPrimitive)soapObject.getProperty(0);

			// 若返回值是单个值，则只能用SoapPrimitive取，此时若用SoapObject强制转换会出异常
			if (envelope.getResponse() != null) {

				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

				result = Boolean.valueOf(response.toString());

			}

		} catch (Exception e) {
			e.printStackTrace();

		}

		return result;

	}

	// 修改用户密码
	public static String ChgPW(String url, String sql) {
		// Configuration cf = new Configuration();
		// url = cf.getValue("url");

		String soapAction = NameSpace + MethodExcute;
		String result = "";

		try {

			// 指定WebService的命名空间和调用的方法名

			SoapObject request = new SoapObject(NameSpace, MethodExcute);
			// step2设置调用方法的参数值

			request.addProperty("strSQL", sql);

			// 生成调用WebService方法的SOAP请求信息。
			// 该信息由SoapSerializationEnvelope对象描述

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

			SoapEnvelope.VER11);
			// 第1步创建的SoapObject对象。
			envelope.bodyOut = request;

			// 设置webservice是.Net
			envelope.dotNet = true;

			// 等价于envelope.bodyOut = request;
			// envelope.setOutputSoapObject(request);

			// step4 创建HttpTransportSE对象,指定url

			HttpTransportSE ht = new HttpTransportSE(url);

			// call方法调用WebService方法

			ht.call(soapAction, envelope);

			// 若返回值有多个，则用SoapObject取，用getProperty来获得每个返回值
			// SoapObject soapObject = (SoapObject) envelope.getResponse();
			// SoapPrimitive value = (SoapPrimitive)soapObject.getProperty(0);

			// 若返回值是单个值，则只能用SoapPrimitive取，此时若用SoapObject强制转换会出异常
			if (envelope.getResponse() != null) {

				Object response = envelope.getResponse();

				result = response.toString();

			}

		} catch (Exception e) {
			e.printStackTrace();

		}

		return result;

	}

	// 获取图片二进制字符串
	public static String WSImage(String url, String strpath) {

		// Configuration cf = new Configuration();
		// url = cf.getValue("url");
		String soapAction = NameSpace + MethodImage;
		String result = "";

		try {

			// 指定WebService的命名空间和调用的方法名

			SoapObject request = new SoapObject(NameSpace, MethodImage);
			// step2设置调用方法的参数值

			request.addProperty("requestFileName", strpath);

			// 生成调用WebService方法的SOAP请求信息。
			// 该信息由SoapSerializationEnvelope对象描述

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

			SoapEnvelope.VER11);
			// 第1步创建的SoapObject对象。
			envelope.bodyOut = request;
			// (new MarshalBase64()).register(envelope);//

			// 设置webservice是.Net
			envelope.dotNet = true;

			// 等于envelope.bodyOut = request;

			// envelope.setOutputSoapObject(request);

			// step4 创建HttpTransportSE对象,指定url

			HttpTransportSE ht = new HttpTransportSE(url);

			// call方法调用WebService方法

			// ht.call(soapAction, envelope);

			// //防止连接时间过期////////////////////////////

			try {
				Thread.sleep(300);
				ht.call(soapAction, envelope);

			} catch (SocketTimeoutException e1) {
				// TODO Auto-generated catch block
				try {
					ht.call(soapAction, envelope);
				} catch (SocketTimeoutException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			// //////////////////////////////

			// 若返回值有多个，则用SoapObject取，用getProperty来获得每个返回值
			// SoapObject soapObject = (SoapObject) envelope.getResponse();
			// SoapPrimitive value = (SoapPrimitive)soapObject.getProperty(0);

			// 若返回值是单个值，则只能用SoapPrimitive取，此时若用SoapObject强制转换会出异常
			if (envelope.getResponse() != null) {

				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

				result = response.toString();
			}

		} catch (Exception e) {

			result = e.getMessage();

		}

		return result;

	}

	// 获取图片二进制字符串
	public static String getWebsericeUrl(Context ct, String SET_NAME) {
		SharedPreferences settings = ct.getSharedPreferences(SET_NAME, 0);
		String s1 = "";
		s1 = settings.getString("url", "");

		return s1;

	}

}
